package com.eorchis.module.newsuserlink.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.stereotype.Repository;

import com.eorchis.constants.SystemConstants;
import com.eorchis.core.basedao.dao.impl.HibernateAbstractBaseDao;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.basedao.query.condition.builder.CompareType;
import com.eorchis.core.basedao.query.condition.builder.IConditionBuilder;
import com.eorchis.core.ui.commond.IQueryCommond;
import com.eorchis.module.news.domain.News;
import com.eorchis.module.news.ui.commond.NewsValidCommond;
import com.eorchis.module.newsuserlink.dao.INewsUserLinkDao;
import com.eorchis.module.newsuserlink.domain.NewsUserLink;
import com.eorchis.module.newsuserlink.ui.commond.NewsUserLinkQueryCommond;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: NewsUserLinkDaoImpl<br>
 * Description: 消息用户关联表数据接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author RongSL
 * @createDate 2016-05-06
 * @version $Revision$
 */
@SuppressWarnings("unchecked")
@Repository(value = "com.eorchis.module.newsuserlink.dao.impl.NewsUserLinkDaoImpl")
public class NewsUserLinkDaoImpl extends HibernateAbstractBaseDao implements INewsUserLinkDao {
	
	@Override
	public Class<? extends IBaseEntity> entityClass() {
		return NewsUserLink.class;
	}

	@Override
	public void queryConditionProcessor(
			IConditionBuilder defaultQueryConditionBuilder,
			IQueryCommond queryCommond) {
		NewsUserLinkQueryCommond newsUserLinkQueryCommond = (NewsUserLinkQueryCommond)queryCommond;
		defaultQueryConditionBuilder.setBaseQueryString("SELECT t FROM NewsUserLink t");
		defaultQueryConditionBuilder.addCondition("t.newsUserId", CompareType.IN, newsUserLinkQueryCommond.getSearchNewsUserIds());
		defaultQueryConditionBuilder.addCondition("t.readState", CompareType.LIKE,newsUserLinkQueryCommond.getSearchReadState());
		defaultQueryConditionBuilder.addCondition("t.replyState", CompareType.LIKE,newsUserLinkQueryCommond.getSearchReplyState());
		defaultQueryConditionBuilder.addCondition("t.receiveLookState", CompareType.LIKE,newsUserLinkQueryCommond.getSearchReceiveLookState());
		defaultQueryConditionBuilder.addCondition("t.reply", CompareType.LIKE,newsUserLinkQueryCommond.getSearchReply());
	}

	@Override
	public List<NewsUserLink> NewUserList(NewsUserLinkQueryCommond commond) {
		return null;
	}
	
	/**
	 * 批量发送消息
	 * @author RongSL
	 * 2017年12月12日
	 * */
	@SuppressWarnings("deprecation")
	public Boolean batchSaveNewsUserLink(NewsValidCommond commond) throws Exception{
		boolean flag=true;
		Map<String,Object> params=new HashMap<String,Object>();
		try{
			StringBuffer sql=new StringBuffer(" select distinct udu.userid from "+SystemConstants.SYSTEM_CODE+"uum_department_user udu "
					+ "where udu.depid in ( select ud.depid from "+SystemConstants.SYSTEM_CODE+"uum_department ud where ud.treepath like :treepath and ud.active_state=1) "
					+ " and udu.active_state=1 and udu.userid<>:userId ");
			if(PropertyUtil.objectNotEmpty(commond.getNewsTypeCode())){
				if(News.NEWS_TYPE_CODE_XUEYUAN.equals(commond.getNewsTypeCode())){
					sql.append(" and udu.userid not in ( select udu.userid from  "+SystemConstants.SYSTEM_CODE+"uum_sys_dep_sysrole_grp_view udu ) ");
				}else if(News.NEWS_TYPE_CODE_ADMIN.equals(commond.getNewsTypeCode())){
					sql.append(" and udu.userid in ( select udu.userid from  "+SystemConstants.SYSTEM_CODE+"uum_sys_dep_sysrole_grp_view udu ) ");
				}
			}
			params.put("treepath", commond.getTreepath()+"%");
			params.put("userId", commond.getSendUserId());
			List<?> list=this.executeFind(QueryStringType.SQL, sql.toString(), params);
			if(!list.isEmpty()){
				Connection con=this.getSession().connection();
				PreparedStatement ppst=con.prepareStatement("insert into NEWS_USER_LINK values (?,?,?,?,now(),?,?,?,?,?)");
				ppst.setInt(2, News.IS_N);//未读取
				ppst.setInt(3, News.IS_N);//未回复
				ppst.setInt(4, News.IS_Y);//显示
				ppst.setString(5, commond.getNewsId());
				ppst.setInt(6, News.IS_Y);//活动
				ppst.setString(7,null);
				ppst.setString(9,null);
				Iterator<?> ite=list.iterator();
				while(ite.hasNext()){
					ppst.setString(1, UUID.randomUUID().toString().replace("-", ""));
					ppst.setObject(8, ite.next());
					ppst.addBatch();
				}
				ppst.executeBatch();
				flag=true;
			}
		}catch(Exception e){
			System.out.println("保存失败！");
			e.printStackTrace();
			flag=false;
		}
		return flag;
	}
}
